<?php

App::uses('Crud', 'Model');
App::uses('Certification', 'Model');

class Workstation extends Crud {

    /** Al Agregar/eliminar/actualizar las constantes de los modelos implica ir
     * y actualizar los comentarios en los campos de la tabla asociada a este modelo.
     */
    const STATUS_ENABLED = 1;
    const STATUS_DISABLED = 0;
    const OPTIONAL_YES = 1;
    const OPTIONAL_NO = 0;

    /**
     * Obtenemos los registros para el grid del admin
     * @return array
     */
    public function getEnabled() {
        $order = array('name' => 'ASC');
        $records = $this->findAllByStatus(self::STATUS_ENABLED, array(), $order);
        return $this->flatArray($records);
    }

    /**
     * Buscamos un area por su nombre
     * @param string $name
     * @return array
     */
    public function getByName($name) {
        return $this->find('first', array('conditions' => array(
                        'name' => $name
        )));
    }

    /**
     * Obtenemos las Operaciones por linea
     * @param string $lineId
     * @return array
     */
    public function getEnabledByLine($lineId) {
        $filters = array(
            'fields' => array(
                'id',
                'name',
                'description',
                '(days / 7) as vigenciaProccess',
                '(max_days / 7) as maxProccess',
                '(life / 365) as vigenciaCertification',
                'notification_end',
                'notification_life',
                'optional',
                'image as img',
                'lock_days'
            ),
            'conditions' => array(
                'status' => self::STATUS_ENABLED,
                'line_id' => $lineId,
            ),
            'order' => array(
                'name' => 'ASC'
            ),
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }

    public function getEnabledByLineForMh($lineId) {
        $filters = array(
            'fields' => array(
                'name as num',
                'description as name',
                'id as id',
                'optional',
                'days',
                'max_days',
                'id',
            ),
            'conditions' => array(
                'status' => self::STATUS_ENABLED,
                'line_id' => $lineId,
            ),
            'order' => array(
                'num' => 'ASC'
            ),
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }

    public function getEnabledByWorkstationWithCertification($workstationId, $userId)
    {
        $filters = array(
            'fields' => array(
                'Certification.id as cId',
                'Certification.start as cStart',
                'Certification.end as cEnd',
                'Certification.life as cLife',
                'Workstation.id as wId',
                'Workstation.description as wDescription',
                'Workstation.life as wLife',
                'Workstation.days as wDays',
                'u.id as uId',
                'u.full_name as uName',
            ),
            'joins' => array(
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'u',
                    'type' => 'left',
                    'conditions' => array(
                        'u.id' => $userId,
                    ),
                ),
            ),
            'conditions' => array(
                'Workstation.status' => self::STATUS_ENABLED,
                'Workstation.id' => $workstationId,
            ),
            'order' => array(
                'Workstation.name' => 'ASC'
            ),
        );
        return $this->find('first', $filters);
    }

    public function getByLine($lineId) {
        $filters = array(
            'fields' => array(
                'id',
                'name',
                'description',
                '(days / 365) as years',
            ),
            'conditions' => array(
                'line_id' => $lineId,
            ),
            'order' => array(
                'name' => 'ASC'
            ),
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }

    /**
     * Obtenemos una estación por la linea y la descripción de la estación
     * @param string $lineId UUID
     * @param string $description UUID
     * @return array
     */
    public function getByLineAndDescription($lineId, $description) {
        $filters = array(
            'fields' => array(
                'id',
            ),
            'conditions' => array(
                'line_id' => $lineId,
                'description' => $description,
            )
        );
        return $this->find('first', $filters);
    }

    public function getEnabledByLineBasic($lineId) {
        $filters = array(
            'fields' => array(
                'id as value',
                'description as text',
            ),
            'conditions' => array(
                'status' => self::STATUS_ENABLED,
                'line_id' => $lineId,
            ),
            'order' => array(
                'name' => 'ASC'
            ),
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }
    
    public function getEnabledWithHierarchy() {
        $filters = array(
            'fields' => array(
                'Workstation.id as value',
                'Workstation.description as text',
                'l.id as line_id',
                'a.id as area_id',
                'm.id as moe_id'
            ),
            'joins' => array(
                array(
                    'table' => 'lines',
                    'alias' => 'l',
                    'type' => 'inner',
                    'conditions' => array(
                        'l.id = line_id',
                    ),
                ),
                array(
                    'table' => 'areas',
                    'alias' => 'a',
                    'type' => 'inner',
                    'conditions' => array(
                        'a.id = l.area_id',
                    ),
                ),
                array(
                    'table' => 'moes',
                    'alias' => 'm',
                    'type' => 'inner',
                    'conditions' => array(
                        'm.id = a.moe_id',
                    ),
                ),
            ), 
            'conditions' => array(
                'Workstation.status' => self::STATUS_ENABLED,
            ),
            'order' => array(
                'Workstation.name' => 'ASC'
            ),
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }
}
